home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 39 / Amiga Format CD39 (1999-04-13)(Future Publishing)(GB)[!][issue 1999-05].iso / -seriously_amiga- / graphics / ripley / source / spatscal.c < prev    next >
C/C++ Source or Header  |  1999-03-02  |  10KB  |  334 lines

  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include "config.h"
  6. #include "global.h"
  7.  
  8. /* private prototypes */
  9. static void Read_Lower_Layer_Component_Framewise _ANSI_ARGS_((int comp, int lw, int lh));
  10. static void Read_Lower_Layer_Component_Fieldwise _ANSI_ARGS_((int comp, int lw, int lh));
  11. static void Make_Spatial_Prediction_Frame _ANSI_ARGS_((int progressive_frame,
  12.   int llprogressive_frame, unsigned char *fld0, unsigned char *fld1, 
  13.   short *tmp, unsigned char *dst, int llx0, int lly0, int llw, int llh, 
  14.   int horizontal_size, int vertical_size, int vm, int vn, int hm, int hn, 
  15.   int aperture));
  16. static void Deinterlace _ANSI_ARGS_((unsigned char *fld0, unsigned char *fld1,
  17.   int j0, int lx, int ly, int aperture));
  18. static void Subsample_Vertical _ANSI_ARGS_((unsigned char *s, short *d,
  19.   int lx, int lys, int lyd, int m, int n, int j0, int dj));
  20. static void Subsample_Horizontal _ANSI_ARGS_((short *s, unsigned char *d,
  21.   int x0, int lx, int lxs, int lxd, int ly, int m, int n));
  22.  
  23.  
  24.  
  25. /* get reference frame */
  26. void Spatial_Prediction()
  27. {
  28.   
  29.   if(Frame_Store_Flag)
  30.   {
  31.     Read_Lower_Layer_Component_Framewise(0,lower_layer_prediction_horizontal_size, 
  32.       lower_layer_prediction_vertical_size);      /* Y */
  33.     Read_Lower_Layer_Component_Framewise(1,lower_layer_prediction_horizontal_size>>1,
  34.       lower_layer_prediction_vertical_size>>1);   /* Cb ("U") */
  35.     Read_Lower_Layer_Component_Framewise(2,lower_layer_prediction_horizontal_size>>1,
  36.       lower_layer_prediction_vertical_size>>1);   /* Cr ("V") */
  37.   }
  38.   else
  39.   {
  40.     Read_Lower_Layer_Component_Fieldwise(0,lower_layer_prediction_horizontal_size, 
  41.       lower_layer_prediction_vertical_size);      /* Y */
  42.     Read_Lower_Layer_Component_Fieldwise(1,lower_layer_prediction_horizontal_size>>1,
  43.       lower_layer_prediction_vertical_size>>1);   /* Cb ("U") */
  44.     Read_Lower_Layer_Component_Fieldwise(2,lower_layer_prediction_horizontal_size>>1,
  45.       lower_layer_prediction_vertical_size>>1);   /* Cr ("V") */
  46.   }
  47.  
  48.  
  49.   Make_Spatial_Prediction_Frame  /* Y */
  50.     (progressive_frame,lower_layer_progressive_frame,llframe0[0],llframe1[0],
  51.      lltmp,current_frame[0],lower_layer_horizontal_offset,
  52.      lower_layer_vertical_offset,
  53.      lower_layer_prediction_horizontal_size,
  54.      lower_layer_prediction_vertical_size,
  55.      horizontal_size,vertical_size,vertical_subsampling_factor_m,
  56.      vertical_subsampling_factor_n,horizontal_subsampling_factor_m,
  57.      horizontal_subsampling_factor_n,
  58.      picture_structure!=FRAME_PICTURE); /* this changed from CD to DIS */
  59.  
  60.   Make_Spatial_Prediction_Frame  /* Cb */
  61.     (progressive_frame,lower_layer_progressive_frame,llframe0[1],llframe1[1],
  62.      lltmp,current_frame[1],lower_layer_horizontal_offset/2,
  63.      lower_layer_vertical_offset/2,
  64.      lower_layer_prediction_horizontal_size>>1,
  65.      lower_layer_prediction_vertical_size>>1,
  66.      horizontal_size>>1,vertical_size>>1,vertical_subsampling_factor_m,
  67.      vertical_subsampling_factor_n,horizontal_subsampling_factor_m,
  68.      horizontal_subsampling_factor_n,1);
  69.  
  70.   Make_Spatial_Prediction_Frame  /* Cr */
  71.     (progressive_frame,lower_layer_progressive_frame,llframe0[2],llframe1[2],
  72.      lltmp,current_frame[2],lower_layer_horizontal_offset/2,
  73.      lower_layer_vertical_offset/2,
  74.      lower_layer_prediction_horizontal_size>>1,
  75.      lower_layer_prediction_vertical_size>>1,
  76.      horizontal_size>>1,vertical_size>>1,vertical_subsampling_factor_m,
  77.      vertical_subsampling_factor_n,horizontal_subsampling_factor_m,
  78.      horizontal_subsampling_factor_n,1);
  79.  
  80. }
  81.  
  82. static void Read_Lower_Layer_Component_Framewise(comp,lw,lh)
  83.      int comp;
  84.      int lw, lh;
  85. {
  86.   FILE *fd;
  87.   char fname[256];
  88.   char ext[3][3] = {".Y",".U",".V"}; 
  89. /*  char *ext = {".Y",".U",".V"}; */
  90.   int i,j;
  91.  
  92.   sprintf(fname,Lower_Layer_Picture_Filename,True_Framenum);
  93.   strcat(fname,ext[comp]);
  94. #ifdef VERBOSE
  95.   if (Verbose_Flag>1)
  96.     printf("reading %s\n",fname);
  97. #endif /* VERBOSE */
  98.   fd=fopen(fname,"rb");
  99.   if (fd==NULL) exit(-1);
  100.   for (j=0; j<lh; j++) {
  101.      for (i=0; i<lw; i++)
  102.        llframe0[comp][lw*j+i]=getc(fd);
  103.      if (! lower_layer_progressive_frame) {
  104.     j++;
  105.     for (i=0; i<lw; i++)
  106.       llframe1[comp][lw*j+i]=getc(fd);
  107.      }
  108.   }
  109.   fclose(fd);
  110. }
  111.  
  112.  
  113. static void Read_Lower_Layer_Component_Fieldwise(comp,lw,lh)
  114.      int comp;
  115.      int lw, lh;
  116. {
  117.   FILE *fd;
  118.   char fname[256];
  119.   char ext[3][3] = {".Y",".U",".V"}; 
  120. /*  char *ext = {".Y",".U",".V"}; */
  121.   int i,j;
  122.  
  123.   sprintf(fname,Lower_Layer_Picture_Filename,True_Framenum,lower_layer_progressive_frame ? 'f':'a');
  124.   strcat(fname,ext[comp]);
  125. #ifdef VERBOSE
  126.   if (Verbose_Flag>1)
  127.     printf("reading %s\n",fname);
  128. #endif /* VERBOSE */
  129.   fd=fopen(fname,"rb");
  130.   if (fd==NULL) exit(-1);
  131.   for (j=0; j<lh; j+=lower_layer_progressive_frame?1:2)
  132.     for (i=0; i<lw; i++)
  133.       llframe0[comp][lw*j+i]=getc(fd);
  134.   fclose(fd);
  135.  
  136.   if (! lower_layer_progressive_frame) {
  137.     sprintf(fname,Lower_Layer_Picture_Filename,True_Framenum,'b');
  138.     strcat(fname,ext[comp]);
  139. #ifdef VERBOSE
  140.     if (Verbose_Flag>1)
  141.       printf("reading %s\n",fname);
  142. #endif /* VERBOSE */
  143.     fd=fopen(fname,"rb");
  144.     if (fd==NULL) exit(-1);
  145.     for (j=1; j<lh; j+=2)
  146.       for (i=0; i<lw; i++)
  147.         llframe1[comp][lw*j+i]=getc(fd);
  148.     fclose(fd);
  149.   }
  150. }
  151.  
  152.  
  153. /* form spatial prediction */
  154. static void Make_Spatial_Prediction_Frame(progressive_frame,
  155.   llprogressive_frame,fld0,fld1,tmp,dst,llx0,lly0,llw,llh,horizontal_size,
  156.   vertical_size,vm,vn,hm,hn,aperture)
  157. int progressive_frame,llprogressive_frame;
  158. unsigned char *fld0,*fld1;
  159. short *tmp;
  160. unsigned char *dst;
  161. int llx0,lly0,llw,llh,horizontal_size,vertical_size,vm,vn,hm,hn,aperture;
  162. {
  163.   int w, h, x0, llw2, llh2;
  164.  
  165.   llw2 = (llw*hn)/hm;
  166.   llh2 = (llh*vn)/vm;
  167.  
  168.   if (llprogressive_frame)
  169.   {
  170.     /* progressive -> progressive / interlaced */
  171.     Subsample_Vertical(fld0,tmp,llw,llh,llh2,vm,vn,0,1);
  172.   }
  173.   else if (progressive_frame)
  174.   {
  175.     /* interlaced -> progressive */
  176.     if (lower_layer_deinterlaced_field_select)
  177.     {
  178.       Deinterlace(fld1,fld0,0,llw,llh,aperture);
  179.       Subsample_Vertical(fld1,tmp,llw,llh,llh2,vm,vn,0,1);
  180.     }
  181.     else
  182.     {
  183.       Deinterlace(fld0,fld1,1,llw,llh,aperture);
  184.       Subsample_Vertical(fld0,tmp,llw,llh,llh2,vm,vn,0,1);
  185.     }
  186.   }
  187.   else
  188.   {
  189.     /* interlaced -> interlaced */
  190.     Deinterlace(fld0,fld1,1,llw,llh,aperture);
  191.     Deinterlace(fld1,fld0,0,llw,llh,aperture);
  192.     Subsample_Vertical(fld0,tmp,llw,llh,llh2,vm,vn,0,2);
  193.     Subsample_Vertical(fld1,tmp,llw,llh,llh2,vm,vn,1,2);
  194.   }
  195.  
  196.     /* vertical limits */
  197.     if (lly0<0)
  198.     {
  199.       tmp-= llw*lly0;
  200.       llh2+= lly0;
  201.       if (llh2<0)
  202.         llh2 = 0;
  203.       h = (vertical_size<llh2) ? vertical_size : llh2;
  204.     }
  205.     else
  206.     {
  207.       dst+= horizontal_size*lly0;
  208.       h= vertical_size - lly0;
  209.       if (h>llh2)
  210.         h = llh2;
  211.     }
  212.  
  213.     /* horizontal limits */
  214.     if (llx0<0)
  215.     {
  216.       x0 = -llx0;
  217.       llw2+= llx0;
  218.       if (llw2<0)
  219.         llw2 = 0;
  220.       w = (horizontal_size<llw2) ? horizontal_size : llw2;
  221.     }
  222.     else
  223.     {
  224.       dst+= llx0;
  225.       x0 = 0;
  226.       w = horizontal_size - llx0;
  227.       if (w>llw2)
  228.         w = llw2;
  229.     }
  230.   
  231.   Subsample_Horizontal(tmp,dst,x0,w,llw,horizontal_size,h,hm,hn);
  232. }
  233.  
  234. /* deinterlace one field (interpolate opposite parity samples)
  235.  *
  236.  * deinterlacing is done in-place: if j0=1, fld0 contains the input field in
  237.  * its even lines and the odd lines are interpolated by this routine
  238.  * if j0=0, the input field is in the odd lines and the even lines are
  239.  * interpolated
  240.  *
  241.  * fld0: field to be deinterlaced
  242.  * fld1: other field (referenced by the two field aperture filter)
  243.  * j0:   0: interpolate even (top) lines, 1: interpolate odd (bottom) lines
  244.  * lx:   width of fld0 and fld1
  245.  * ly:   height of the deinterlaced field (has to be even)
  246.  * aperture: 1: use one field aperture filter (two field otherwise)
  247.  */
  248. static void Deinterlace(fld0,fld1,j0,lx,ly,aperture)
  249. unsigned char *fld0,*fld1;
  250. int j0,lx,ly; /* ly has to be even */
  251. int aperture;
  252. {
  253.   int i,j,v;
  254.   unsigned char *p0, *p0m1, *p0p1, *p1, *p1m2, *p1p2;
  255.  
  256.   /* deinterlace one field */
  257.   for (j=j0; j<ly; j+=2)
  258.   {
  259.     p0 = fld0+lx*j;
  260.     p0m1 = (j==0)    ? p0+lx : p0-lx;
  261.     p0p1 = (j==ly-1) ? p0-lx : p0+lx;
  262.  
  263.     if (aperture)
  264.       for (i=0; i<lx; i++)
  265.         p0[i] = (unsigned int)(p0m1[i] + p0p1[i] + 1)>>1;
  266.     else
  267.     {
  268.       p1 = fld1 + lx*j;
  269.       p1m2 = (j<2)     ? p1 : p1-2*lx;
  270.       p1p2 = (j>=ly-2) ? p1 : p1+2*lx;
  271.       for (i=0; i<lx; i++)
  272.       {
  273.         v = 8*(p0m1[i]+p0p1[i]) + 2*p1[i] - p1m2[i] - p1p2[i];
  274.         p0[i] = Clip[(v + ((v>=0) ? 8 : 7))>>4];
  275.       }
  276.     }
  277.   }
  278. }
  279.  
  280. /* vertical resampling */
  281. static void Subsample_Vertical(s,d,lx,lys,lyd,m,n,j0,dj)
  282. unsigned char *s;
  283. short *d;
  284. int lx, lys, lyd, m, n, j0, dj;
  285. {
  286.   int i, j, c1, c2, jd;
  287.   unsigned char *s1, *s2;
  288.   short *d1;
  289.  
  290.   for (j=j0; j<lyd; j+=dj)
  291.   {
  292.     d1 = d + lx*j;
  293.     jd = (j*m)/n;
  294.     s1 = s + lx*jd;
  295.     s2 = (jd<lys-1)? s1+lx : s1;
  296.     c2 = (16*((j*m)%n) + (n>>1))/n;
  297.     c1 = 16 - c2;
  298.     for (i=0; i<lx; i++)
  299.       d1[i] = c1*s1[i] + c2*s2[i];
  300.   }
  301. }
  302.  
  303. /* horizontal resampling */
  304. static void Subsample_Horizontal(s,d,x0,lx,lxs,lxd,ly,m,n)
  305. short *s;
  306. unsigned char *d;
  307. int x0, lx, lxs, lxd, ly, m, n;
  308. {
  309.   int i, i1, j, id, c1, c2, v;
  310.   short *s1, *s2;
  311.   unsigned char *d1;
  312.  
  313.   for (i1=0; i1<lx; i1++)
  314.   {
  315.     d1 = d + i1;
  316.     i = x0 + i1;
  317.     id = (i*m)/n;
  318.     s1 = s+id;
  319.     s2 = (id<lxs-1) ? s1+1 : s1;
  320.     c2 = (16*((i*m)%n) + (n>>1))/n;
  321.     c1 = 16 - c2;
  322.     for (j=0; j<ly; j++)
  323.     {
  324.       v = c1*(*s1) + c2*(*s2);
  325.       *d1 = (v + ((v>=0) ? 128 : 127))>>8;
  326.       d1+= lxd;
  327.       s1+= lxs;
  328.       s2+= lxs;
  329.     }
  330.   }
  331. }
  332.  
  333.  
  334.